package com.admin.service.impl;

import com.admin.common.enums.EnumResult;
import com.admin.common.exception.BizException;
import com.admin.common.shiro.ShiroUtils;
import com.admin.common.util.DateUtils;
import com.admin.domain.ScoreParam;
import com.admin.domain.ScoreParamLog;
import com.admin.domain.SysParam;
import com.admin.mapper.*;
import com.admin.service.ScoreParamService;
import com.admin.vo.PageVO;
import com.admin.vo.ResultVO;
import com.admin.vo.SimpleResultVO;
import com.admin.vo.scoreParam.ScoreParamListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.*;

/**
 * @author yangzhongliang
 * @date 2022/05/16
 */
@Slf4j
@Service
public class ScoreParamServiceImpl implements ScoreParamService {
    @Autowired
    private ScoreParamMapper scoreParamMapper;
    @Autowired
    private ScoreParamLogMapper scoreParamLogMapper;

    @Override
    public List<ScoreParam> selectAll() {
        return scoreParamMapper.selectByMap(null);
    }

    @Override
    public ResultVO<PageVO<ScoreParamListVO>> getPageList(Integer pageIndex, Integer pageSize, Map<String, Object> params) {
        ResultVO<PageVO<ScoreParamListVO>> result = new ResultVO<>();
        PageVO<ScoreParamListVO> page = new PageVO<>(pageIndex, pageSize);
        List<ScoreParamListVO> scoreParamListVOList = new ArrayList<>();
        // 查询总数
        params.put("parentId", 0);
        Integer totalCount = scoreParamMapper.countByMap(params);
        page.setTotalCount(totalCount);
        if (totalCount > 0) {
            // 查询管理员信息
            params.put("startIndex", page.getStartIndex());
            params.put("pageSize", page.getPageSize());
            List<ScoreParam> scoreParamList = scoreParamMapper.selectByMap(params);
            if (!CollectionUtils.isEmpty(scoreParamList)) {
                for (ScoreParam scoreParam : scoreParamList) {
                    ScoreParamListVO itemVO = new ScoreParamListVO();
                    BeanUtils.copyProperties(scoreParam, itemVO);
                    //明细
                    List<ScoreParamListVO> subListVO = new ArrayList<>();
                    Map<String,Object> subParams = new HashMap<>();
                    subParams.put("parentId", scoreParam.getId());
                    List<ScoreParam> subList = scoreParamMapper.selectByMap(subParams);
                    if (!CollectionUtils.isEmpty(subList)) {
                        for (ScoreParam param : subList) {
                            ScoreParamListVO subItemVO = new ScoreParamListVO();
                            BeanUtils.copyProperties(param, subItemVO);
                            subListVO.add(subItemVO);
                        }
                    }
                    itemVO.setSubItems(subListVO);
                    scoreParamListVOList.add(itemVO);
                }
            }
        }
        page.setData(scoreParamListVOList);
        result.setData(page);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @Override
    public ResultVO<PageVO<ScoreParamLog>> getLogPageList(Integer pageIndex, Integer pageSize, Map<String, Object> params) {
        ResultVO<PageVO<ScoreParamLog>> result = new ResultVO<>();
        PageVO<ScoreParamLog> page = new PageVO<>(pageIndex, pageSize);
        List<ScoreParamLog>  list = new ArrayList<>();
        Integer totalCount = scoreParamLogMapper.countByMap(params);
        page.setTotalCount(totalCount);
        if (totalCount > 0) {
            // 查询管理员信息
            params.put("startIndex", page.getStartIndex());
            params.put("pageSize", page.getPageSize());
            list = scoreParamLogMapper.selectByMap(params);
            for (ScoreParamLog scoreParamLog : list) {
                scoreParamLog.setCreateTimeStr(DateUtils.format(scoreParamLog.getCreateTime(), DateUtils.DATE_PATTERN_TIME));
            }
        }
        page.setData(list);
        result.setData(page);
        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @Override
    public ScoreParam getById(Integer id) {
        return scoreParamMapper.selectByPrimaryKey(id);
    }


    @Transactional
    @Override
    public SimpleResultVO update(ScoreParam scoreParam) {
        SimpleResultVO result = new SimpleResultVO();
        if (scoreParam == null || scoreParam.getId() == null) {
            throw new BizException(EnumResult.PARAM_NULL.getCode(), EnumResult.PARAM_NULL.getMsg());
        }

        ScoreParam param = scoreParamMapper.selectByPrimaryKey(scoreParam.getId());
        if (Objects.isNull(param)) {
            throw new BizException(-1, "参数无效或数据已删除!");
        }

        ScoreParam parentParam = scoreParamMapper.selectByPrimaryKey(param.getParentId());

        ScoreParam updateParam = new ScoreParam();
        updateParam.setId(param.getId());
        updateParam.setModuleScore(scoreParam.getModuleScore());
        updateParam.setScoreRule(scoreParam.getScoreRule());
        updateParam.setModuleExplain(scoreParam.getModuleExplain());
        updateParam.setRemark(scoreParam.getRemark());
        scoreParamMapper.updateByPrimaryKeySelective(updateParam);

        //更新主表分数
        Double score = scoreParamMapper.getModuleScore(param.getParentId());
        scoreParamMapper.updateMainScore(score, param.getParentId());

        //日志记录表
        ScoreParamLog scoreParamLog = new ScoreParamLog();
        scoreParamLog.setModuleCode(parentParam.getModuleCode());
        scoreParamLog.setModuleName(parentParam.getModuleName());

        scoreParamLog.setModuleSubCode(param.getModuleCode());
        scoreParamLog.setModuleSubName(param.getModuleName());
        StringBuffer scoreB = new StringBuffer();
        scoreB.append(param.getModuleScore()).append("分,修改为:").append(updateParam.getModuleScore()).append("分");
        scoreParamLog.setModuleScore(scoreB.toString());
        scoreParamLog.setScoreRule(updateParam.getScoreRule());
        scoreParamLog.setModuleExplain(updateParam.getModuleExplain());
        scoreParamLog.setRemark(updateParam.getRemark());

        scoreParamLog.setOperatorId(ShiroUtils.getUserId());
        scoreParamLog.setOperatorMobile(ShiroUtils.getMobile());
        scoreParamLog.setOperatorName(ShiroUtils.getRealname());
        scoreParamLogMapper.insertSelective(scoreParamLog);

        result.setCode(EnumResult.SUCCESS.getCode());
        result.setMsg(EnumResult.SUCCESS.getMsg());
        return result;
    }

    @Override
    public ScoreParam getByModuleName(String moduleName) {
        return scoreParamMapper.getByModuleName(moduleName);
    }


    @Override
    public void initData() {
    }
}
